

<template>
    <div style=" height:100%;width:100%;position:relative;">
        <div class="pagetop">
            <!-- <div class="search-title">快速查询</div>
            <el-input v-model="txtsearch" placeholder="代码|输入码|名称|简称" style="width:200px" clearable @keyup.enter.native='btnQuery' :disabled="changeflag"></el-input> -->
            <div class="top-deal">
                <el-button @click="btnQuery" icon="el-icon-search" type="primary" :disabled="changeflag" v-has="'PINQUERY'">查询</el-button>
                <el-button @click="btnAdd" icon="el-icon-plus">新增</el-button>
                <el-button @click="btnDel" icon="el-icon-delete">删除</el-button>
                <el-button @click="btnSave" icon="el-icon-document" :disabled="!changeflag">保存</el-button>
                <el-button @click="btnCancel" icon="el-icon-close" :disabled="!changeflag">取消</el-button>
            </div>
        </div>
        <div class="pagecenter">
            <vxe-table :data="barcodetransdata" :loading="bLoading" :keyboard-config="keyConfig" :select-config="selectConfig" :sort-config="{trigger: 'cell'}" :edit-config="{trigger: 'click',mode: 'cell',showStatus: true,activeMethod: activeMethod,showIcon: false,}" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column ref="xTable" resizable show-header-overflow="tooltip" show-overflow size="mini" stripe :row-class-name="rowClassName" :cell-class-name="cellClassName" @cell-dblclick="celldblOpen" @edit-closed="cellClose" @current-change="currentChangeEvent">
                <vxe-table-column type="index" title="NO." width="50" align="center"></vxe-table-column>
                <vxe-table-column field="barcodetype" title="条码类型" width="90" :edit-render="{name:'select', options: $opt.barcodetypeOption,events:{change:(row,val) => {row.row.barcodetype = val.target.value;hasChange()}}}" align="center"></vxe-table-column>
                <vxe-table-column field="seq" title="处理顺序" width="80" :edit-render="{name:'input', autoselect: true,events: {change: hasChange}}" align="center"></vxe-table-column>
                <vxe-table-column field="condition" title="条件" width="200" :edit-render="{name:'select', autoselect: true,options:$opt.conditionOption,events:{change:(row,val) => {row.row.condition = val.target.value;hasChange()}}}"></vxe-table-column>
                <vxe-table-column field="cond_val1" title="条件取值1" width="150" :edit-render="{name:'input', autoselect: true,events: {change: hasChange}}"></vxe-table-column>
                <vxe-table-column field="transtype" title="处理方式" width="150" :edit-render="{name:'select', options: $opt.transtypeOption,events:{change:(row,val) => {row.row.transtype = val.target.value;hasChange()}}}"></vxe-table-column>
                <vxe-table-column field="transval1" title="处理取值1" width="150" header-align="center" :edit-render="{name:'input', autoselect: true,events: {change: hasChange}}"></vxe-table-column>
                <vxe-table-column field="transval2" title="处理取值2" width="150" :edit-render="{name:'input', autoselect: true,events: {change: hasChange}}"></vxe-table-column>
            </vxe-table>
        </div>

        <!--开发编码-->
        <el-dialog :title="dialogtitle" :visible.sync="dialogOpen" width="700px" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false">
            <div style="height:100%;width: 100%;">
                <el-input ref="popcom" type="textarea" :autosize="{ minRows: 8, maxRows: 10}" v-model="edittext" id="formula_id"></el-input>
            </div>
            <div style="margin-top:10px">
                <table>
                    <tr>
                        <td>
                            <el-button style="width:80px;margin-bottom:5px;" @click="btnaddevent(' [@barcode]');">原条码号</el-button>
                        </td>
                        <td>
                            <el-button style="width:80px;margin-left:10px;margin-bottom:5px;" @click="btnaddevent(' [@hospcode] ');">当前医院</el-button>
                        </td>
                        <td>
                            <el-button style="width:80px;margin-left:10px;margin-bottom:5px;" @click="btnaddevent(' [@ip] ');">IP地址</el-button>
                        </td>
                    </tr>
                </table>
            </div>

            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dialogOpenSave">确 定</el-button>
                <el-button @click="dialogOpen = false">取 消</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script type="text/javascript">

export default {
    name: "MNG239",
    data() {
        return {
            txtsearch: "",
            bLoading: false,
            keyConfig: {
                isDel: true,
                isTab: true,
                isEdit: true,
                isArrow: true,
            },
            keyLocalConfig: {
                isArrow: true,
            },
            dialogOpen: false,
            changeflag: false,
            selectConfig: { tigger: "row" },
            AddData: [],
            ModData: [],
            curselectrow: "",
            fieldcode: "",
            edittext: "",
            dialogtitle: "",
            barcodetransdata: [],
        };
    },
    created() {
        this.btnQuery();
    },
    watch: {},
    computed: {},
    mounted() {},
    updated() {},
    methods: {
        //主表查询
        async btnQuery() {
            try {
                this.AddData = [];
                this.ModData = [];
                this.bLoading = true;
                const res = await this.$tool.request({
                    url: "MNG/MNG2/MNG239",
                    method: "post",
                    data: {
                        action: "initPage",
                        txtsearch: this.txtsearch,
                    },
                });
                if (res.data.length > 0) {
                    this.barcodetransdata = res.data;
                    if (res.data.length > 0) this.$refs.xTable.setCurrentRow(res.data[0]);
                } else {
                    this.barcodetransdata = [];
                }
                this.bLoading = false;
            } catch (e) {
                this.bLoading = false;
                this.$alert(e);
            }
        },
        currentChangeEvent({ row, column }) {
            this.warehouseid = row.warehouseid;
        },
        //新增
        btnAdd() {
            this.changeflag = true;
            this.$refs.xTable
                .insertAt(
                    {
                        barcodetype: "LisBarcode",
                        seq: this.$pub.maxseq(this.$refs.xTable),
                        condition: "1",
                        cond_val1: "",
                        cond_val2: "",
                        transtype: "1",
                        transval1: "",
                        transval2: "",
                    },
                    -1
                )
                .then(({ row }) => {
                    this.$refs.xTable.setCurrentRow(row);
                    this.$refs.xTable.setActiveCell(row, "condition");
                    this.$refs.xTable.scrollToRow(row);
                });
        },
        //删除 -- 弹窗提示
        btnDel() {
            let delrow = this.$refs.xTable.getCurrentRow();
            if (delrow) {
                let msg = "您确定要删除这条数据吗？";
                this.$confirm(msg, {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                    dangerouslyUseHTMLString: true,
                })
                    .then(() => {
                        this.$table.DelRecordSelect(this.$refs.xTable, -1, this);
                        this.hasChange();
                    })
                    .catch(() => {});
            } else {
                this.$alert("请选中需要删除的数据", "提示");
            }
        },
        //保存 -- 保存新增，修改，删除
        async btnSave() {
            let data=this.$refs.xTable.tableData.filter(m=>!m.cond_val1);

            if (data.length>0) {
                this.$alert("您有条件取值1未填写，请填写后再提交！", "提示");
                return false;
            }
            try {
                //let data=this.$refs.xTable.afterFulldata;
                //this.$pub.ResetSeq(data,"seq");
                const res = await this.$tool.request({
                    url: "MNG/MNG2/MNG239",
                    method: "post",
                    data: {
                        action: "saveRecord",
                        data: this.$refs.xTable.getRecordset(),
                    },
                });
                if (res.errcode == "1") {
                    this.btnQuery();
                    this.$pub.message(this, "保存成功！", "success");
                    this.changeflag = false;
                } else {
                    this.$alert("保存失败！" + res.errmsg, "提示");
                }
            } catch (e) {
                this.$alert(e);
            }
        },
        //取消
        btnCancel() {
            this.changeflag = false;
            this.btnQuery();
        },
        activeMethod({ row, column }) {
            return true;
        },

        hasChange() {
            this.changeflag = true;
        }, //控制行的颜色
        rowClassName({ row, rowIndex }) {
            let isDirty = this.$refs.xTable.hasRowChange(row);
            if (isDirty) {
                setTimeout(() => {
                    this.$refs.xTable.setCurrentRow(row);
                }, 10);
                return "row-modify";
            }
            if (this.$refs.xTable.hasRowInsert(row)) {
                return "row-new";
            }
        }, //控制列的颜色
        cellClassName({ row, column }) {}, //自动生成拼音码
        cellClose({ row, column }) {},
        //双击检验组开窗
        celldblOpen({ row, column }) {
            this.edittext = "";
            this.fieldcode = column.property;
            this.curselectrow = row;
            if (row.condition == "99" && column.property == "cond_val1") {
                //开发人员编码
                this.dialogtitle = "开发人员编码";
                this.dialogOpen = true;
            } else if (row.transtype == "99" && column.property == "transval1") {
                this.dialogtitle = "开发人员编码";
                this.dialogOpen = true;
            } else if (row.condition == "98" && column.property == "cond_val1") {
                this.dialogtitle = "SQL语句";
                this.dialogOpen = true;
            } else if (row.transtype == "98" && column.property == "transval1") {
                this.dialogtitle = "SQL语句";
                this.dialogOpen = true;
            } else {
                this.dialogtitle = "";
            }
        },
        dialogOpenSave() {
            //条件或处理方式 为开发人员编码
            if (this.dialogtitle == "开发人员编码") {
                if (this.curselectrow.condition == "99" && this.fieldcode == "cond_val1") {
                    this.verifystr();
                } else if (this.curselectrow.transtype == "99" && this.fieldcode == "transval1") {
                    this.verifystr();
                }
            } else {
                this.curselectrow[this.fieldcode] = this.edittext;
                this.dialogOpen = false;
            }
        },
        verifystr() {
            this.$tool
                .request({
                    url: "MNG/MNG2/MNG239",
                    method: "post",
                    data: {
                        action: "checkcalcformula",
                        checkrules: this.edittext,
                    },
                })
                .then((res) => {
                    if (res.flag == "1") {
                        this.curselectrow[this.fieldcode] = this.edittext;
                        this.dialogOpen = false;
                    } else {
                        this.$confirm(res.msg + "，是否确定强制保存?", "计算公式出错", {
                            confirmButtonText: "确定",
                            cancelButtonText: "取消",
                            type: "warning",
                        })
                            .then(() => {
                                this.curselectrow[this.fieldcode] = this.edittext;
                                this.dialogOpen = false;
                            })
                            .catch(() => {});
                    }
                })
                .catch((e) => {
                    this.$message.error(e);
                });
        },
        btnaddevent(addstr) {
            var elInput = document.getElementById("formula_id");
            var startPos = elInput.selectionStart;
            var endPos = elInput.selectionEnd;
            if (startPos === undefined || endPos === undefined) return;
            var txt = elInput.value;
            var result = txt.substring(0, startPos) + addstr + txt.substring(endPos);
            elInput.value = result; // 赋值给input的value
            // 重新定义光标位置
            elInput.focus();
            elInput.selectionStart = startPos + addstr.length;
            elInput.selectionEnd = startPos + addstr.length;
            this.jbxx_s_calc_formula = result;
        },
        //检验工作组改变
        labgroupChange(code, name) {
            this.curselectrow.labgroupid = code;
            this.curselectrow.labgroupname = name;
            this.dialogOpen = false;
            this.hasChange();
            setTimeout(() => {
                this.$refs.xTable.setCurrentRow(this.curselectrow);
            }, 10);
        },
    },
};
</script>
<style scoped>
.pagetop {
    width: 100%;
    height: 35px;
    display: -webkit-box;
    -webkit-box-pack: left;
    -webkit-box-align: center;
    border: 1px solid #ccc;
    position: absolute;
    z-index: 99;
}

.pagecenter {
    position: absolute;
    top: 40px;
    width: 100%;
    left: 0;
    bottom: 0px;
    overflow: auto;
}

.search-title {
    margin: 0 5px 0 20px;
    font-size: 12px;
}
.top-deal {
    right: 5px;
    position: absolute;
    top: 5px;
    display: flex;
    align-items: center;
}
.detaixTable {
}
/deep/.el-textarea__inner {
    display: block;
    resize: vertical;
    padding: 2px 4px;
}
/deep/ .el-button.is-disabled {
    border: 0px;
    color: grey;
    background-color: #e6e6e6;
    font-weight: normal;
}
/deep/ tr.vxe-body--row.row-modify {
    color: blue;
}
/deep/ tr.vxe-body--row.row-new {
    color: #cc6600;
}
/deep/.vxe-body--row,
.vxe-body--row.row--current,
.vxe-body--row.row--hover {
    vertical-align: top;
}
.table1style tr td {
    padding: 0px 2px 0px 2px;
}
</style>